<?php
    class MainController
    {
        private $model;
        private $view;
        
        public function __construct($model, $view)
        {
            $this->model = $model;
            $this->view  = $view;
            $view->setController($this);            
        }
        
        // Parse the actions
        public function parseActions()
        {
            // Check if the configuration-file exists:
            if(!file_exists('config.inc.php')) {
                header('Location: setup/index.php');
            } 
            // See if the user is logged in:
            $loggedIn = isset($_SESSION['loggedIn']) ? $_SESSION['loggedIn'] : false;
            if(!$loggedIn) {
                header('Location: login.php');
            }
            if(isset($_GET['logout'])) {
                unset($_SESSION['loggedIn']);
                header('Location: index.php');
            }            
            // Connect and inititalize:
            include_once('core/init.php');
            // Save & Delete:
            if(isset($_POST['save'])) {
                // Store a new item:
                $item = array();
                $item['name']        = makeSafe($_POST['name']);
                $item['id_category'] = $_POST['id_category'];
                // Store the options:
                $options = array();
                foreach($this->model->getOptions($item['id_category']) as $option) {
                    if($option['hasmany']==1) {
                        // This is a 'hasmany' option:
                        if($_POST['manycounter']>0) {
                            for($i=0; $i<=$_POST['manycounter']; $i++) {
                                if(isset($_POST['option_'.$option['id'].'_m_'.$i])) {
                                    array_push($options, array('id'=>$option['id'], 'value'=>makeSafe($_POST['option_'.$option['id'].'_m_'.$i])));
                                }
                            }
                        }
                    } else {
                        array_push($options, array('id'=>$option['id'], 'value'=>makeSafe($_POST['option_'.$option['id']])));
                    }
                }
                $item['options'] = $options;
                // Store the item:
                if($_POST['action']=='add') {
                    $this->model->addItem($item);
                } elseif($_POST['action']=='edit') {
                    $this->model->updateItem($_POST['id'], $item);
                }
            } elseif(isset($_POST['delete'])) {
                // Delete this item:
                $id = $_POST['id'];
                $this->model->deleteItem($id);
            }
        }
        
        // Render the output
        public function render()
        {
            // Require the categories model:
            include_once('core/categoriesModel.php');
            $categoriesModel = new CategoriesModel();
            // Render the page
            $this->view->startHTML();
            $this->view->showTop();
            $this->view->showSideBar();
            // Render content
            $this->view->startContent();
            if(isset($_GET['options'])) {
                // SHow the options manager:
                include_once('core/options.php');
            } elseif(isset($_GET['datatypes'])) {
                // Show the datatypes manager:
                include_once('core/datatypes.php');
            } elseif(isset($_GET['categories'])) {
                // Show the categories manager:
                include_once('core/categories.php');
            } elseif(isset($_GET['users'])) {
                // Show the users manager:
                include_once('core/users.php');
            } elseif(isset($_GET['cat'])) {
                // Show a single category:
                // Extra security check: See if the category is numeric:
                if(is_numeric($_GET['cat'])) {
                    // Use the categoriesModel to retreive category information:
                    $info = $categoriesModel->getCategory($_GET['cat']);
                    $this->view->showCategory($info);
                    $this->view->startForm($_GET['cat']);
                    if(isset($_POST['add'])) {
                        // Show the form to add a new item:
                        $item           = $this->model->createEmptyItem($_GET['cat']);
                        $item['action'] = 'add';
                        $this->view->showForm($item);
                    } elseif(isset($_POST['edit'])) {
                        // Show the form to edit this item:
                        $id             = $_POST['id'];
                        $item           = $this->model->getItem($id);
                        $item['action'] = 'edit';
                        $this->view->showForm($item);
                    } else {                        
                        // Show all items in this category:
                        $records = $this->model->getRecords($_GET['cat']);
                        if(count($records)==0) {
                            $this->view->showNoRecordsAvailable();    
                        } else {
                            $this->view->showRecords($records);
                        }
                        // Show add/edit/delete buttons:
                        $this->view->showAddEditDelete();
                    }
                    $this->view->endForm();
                }
            }
            $this->view->endContent();
            $this->view->showFooter();
            $this->view->endHTML();
        }
        
        // Get the subcategories of this category
        public function getCategories($id_category)
        {
            include_once('core/categoriesModel.php');
            $categoriesModel = new CategoriesModel();
            return $categoriesModel->getCategories($id_category);
        }
        
        // Get the records
        public function getRecords($id_category)
        {
            return $this->model->getRecords($id_category);
        }
    }
?>